package com.edu.neusoft.elec_shop.controller;

import com.edu.neusoft.elec_shop.domain.Comments;
import com.edu.neusoft.elec_shop.domain.User;
import com.edu.neusoft.elec_shop.service.CommentsService;
import com.edu.neusoft.elec_shop.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/comments")
@Api(tags = "评论管理接口")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    // 查看某商品的评论
    @GetMapping("/{goodsId}")
    @ApiOperation(value = "查看某商品的评论", notes = "根据传入的商品ID，获取该商品对应的所有评论信息")
    public List<Comments> getCommentsByGoodsId(@PathVariable("goodsId") @Valid Long goodsId) {
        return commentsService.getCommentsByGoodsId(goodsId);
    }

    // 删除自己的评论
    @DeleteMapping("/{userId}")
    @ApiOperation(value = "删除自己的评论", notes = "根据传入的用户ID，删除对应的评论信息。注意：仅能删除自己发布的评论")
    public AjaxResult deleteCommentById(@PathVariable("userId")  @Valid Long userId) {
        try {
            commentsService.deleteCommentById(userId);
            return AjaxResult.success("评论删除成功");
        } catch (Exception e) {
            return AjaxResult.error("评论删除失败：" + e.getMessage());
        }
    }
}